package util;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class TimeoutFilter implements Filter {

	public void destroy() {
	}

	public void doFilter(ServletRequest servletRequest,
			ServletResponse servletResponse, FilterChain filterChain)
			throws IOException, ServletException {

		HttpServletRequest request = (HttpServletRequest) servletRequest;
		HttpServletResponse response = (HttpServletResponse) servletResponse;
		HttpSession session = request.getSession(false);

		if (null == session ||session.isNew()) {
			RequestDispatcher rd=request.getRequestDispatcher("/login.jsp");
			request.setAttribute("message", "timeout,please retry again");
			rd.forward(request, response);
		}
		else{
			filterChain.doFilter(servletRequest, servletResponse);
		}
//		if (request.getRequestURI().endsWith("download.do")) {
//			//此处可以针对不同的请求根据用户是否具有权限来做处理
//		}
//		request.getRequestDispatcher("/Error.jsp").
//		forward(request, response);

	}

	public void init(FilterConfig filterConfig)
	throws ServletException {
	}
}
